package com.example.demonov21th.servlet;

import com.example.demonov21th.User;
import com.example.demonov21th.dao.DBUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Login", value = "/login")
public class Login extends HttpServlet {
    private String msg = null;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        String sql = "select * from user where username = ? and password = ?";

        User user = DBUtil.getUser(
                sql,
                req.getParameter("username"),
                req.getParameter("password")
        );
        if(
                user != null
        ){
            msg = "登录成功！";
        }else{
            msg = "登录失败！";
        }
        resp.setContentType("text/html; charset=utf-8");

        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println("<html><body>");
        out.println("<h1>" + msg + "</h1>");
        out.println("</body></html>");
    }
}
